define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'colorpicker'], function ($, undefined, Backend, Table, Form, Colorpicker) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dts/plan/yuanzhou/index' + location.search,
                    add_url: 'dts/plan/yuanzhou/add',
                    edit_url: 'dts/plan/yuanzhou/edit',
                    del_url: 'dts/plan/yuanzhou/del',
                    multi_url: 'dts/plan/yuanzhou/multi',
                    import_url: 'dts/plan/yuanzhou/import',
                    table: 'dts_plan_yuanzhou',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'which_week', title: __('Which_week'), operate: 'LIKE'},
                        {field: 'startdate', title: __('Startdate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'enddate', title: __('Enddate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'keynote', title: __('Keynote'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createby', title: __('Createby'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), 
                            table: table, events: Table.api.events.operate, 
                            /*隐藏自带编辑按钮、条件隐藏 */
							formatter: function(value,row,index){
							    var that = $.extend({},this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del",null);
                                that.table = table;
							    return Table.api.formatter.operate.call(that,value,row,index)
							},
                            buttons: [
                                {
                                    name: 'edit',
                                    title: __('编辑'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-pencil',
                                    extend: 'data-area=\'["95%","95%"]\'',
                                    url: function (row) {
                                        return 'dts/plan/yuanzhou/edit?ids=' + row.id;
                                    },
                                    visible: function (row) { 
                                        let enddate = new Date(row.enddate).getTime() / 1000;
                                        var current = new Date().getTime() / 1000;
                                        return enddate >= current;
                                    }
                                },
                                {
                                    name: 'details',
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-bitbucket',
                                    extend: 'data-area=\'["95%","95%"]\'',
                                    url: function (row) {
                                        return 'dts/plan/yuanzhou/details?ids=' + row.id;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        details: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    
    $('.colorpicker').colorpicker({
        // 初始化打开的时候把默认的颜色渲染上去，否则显示的弹窗选择器颜色不对
        color: function () {
            var color = "#000000";
            var rgb = $("#c-color").css('color').match(/^rgb\(((\d+),\s*(\d+),\s*(\d+))\)$/);
            if (rgb) {
                color = rgb[1];
            }
            return color;
        }
    });

    $(".btn-append").click(function() {
        let index = $("tbody").find('tr').length + 1;

        let html = '<tr><td><P><input style="width: 100px;" type="text" name="row[content]['+index+'][date]" value="" placeholder="例:2020-01-01"/></P></td>';
        for(let i=0;i<4;i++) {
            let temp = '<td><p><div class="input-group">背景：<input style="width: 120px;font-size: 12px;" readonly name="row[content]['+index+'][list]['+i+'][color]" type="text" value=""><img class="colorpicker" data-input="row[content]['+index+'][list]['+i+'][color]" src="/assets/img/colorful.png" height="22" alt=""></div></p><p>内容：<input type="text" name="row[content]['+index+'][list]['+i+'][value]" value=""/></p></td>';
            html = html + temp;
        }
        html = html + '</tr>';

        $("tbody").append(html);
    });

    $("tbody").on("click",".colorpicker", function(){
        $('.colorpicker').colorpicker({}, function (event, obj) {
            // 选择颜色后的回调
            $("input[name='"+$(event).data('input')+"']").css('color', '#' + obj.hex);
            $("input[name='"+$(event).data('input')+"']").val('#' + obj.hex);
            $(event).addClass("active").data("color", '#' + obj.hex);
        }, function (event) {
            // 选择后 关闭选择颜色窗口
            $(event).removeClass("active");
        });
    });

    return Controller;
});
